package com.lm.fastjson.example;

import com.alibaba.fastjson.JSON;
import com.lm.fastjson.entry.Address;
import com.lm.fastjson.entry.Person;

import java.util.ArrayList;
import java.util.List;

public class FastjsonNestedExample {
    public static void main(String[] args) {
        // 创建对象
        List<Person> people = new ArrayList<>();
        people.add(new Person("Alice", 28, new Address("Shanghai", "200000")));
        people.add(new Person("Bob", 35, new Address("Beijing", "100000")));

        // 序列化对象列表
        String jsonString = JSON.toJSONString(people);
        System.out.println("Serialized JSON List: " + jsonString);

        // 反序列化对象列表
        List<Person> deserializedPeople = JSON.parseArray(jsonString, Person.class);
        deserializedPeople.forEach(person ->
                System.out.println("Deserialized Person: " + person.getName() + ", " + person.getAddress().getCity()));
    }
}
